package com.jinbei.common.util;

import com.jinbei.common.contants.Contants;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * @author zflu
 * des对称加密
 * Created by lucky on 2018/8/17.
 */
public class DesUtil {

    /**
     * 算法名称
     */
    private final static String DES = "DES";

    /**
     * 默认key
     */
    private final static String DEFAULT_KEY = "luckymao";

    /**
     * @author zflu
     * @Description   des加密 base64加密返回
     * @date 2018/8/17 16:35
     * @param data
     * @return java.lang.String
     */
    public static String encrypt(String data){
        return encrypt(data, DEFAULT_KEY);
    }

    /**
     * @author zflu
     * @Description  des加密 base64加密返回
     * @date 2018/8/17 16:33
     * @param data, key
     * @return java.lang.String
     */
    public static String encrypt(String data , String key){
        String result = null;
        try {
            byte[] bt = encryptByte(data.getBytes(Contants.CHARSET_NAME), key.getBytes(Contants.CHARSET_NAME));
            result = Base64.getEncoder().encodeToString(bt);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   正式加密的方法
     * @date 2018/8/17 17:47
     * @param data, key
     * @return byte[]
     */
    public static byte[] encryptByte(byte[] data , byte[] key) throws Exception {

        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        //从原始密钥数据创建DESKeySpec对象
        DESKeySpec des = new DESKeySpec(key);

        //使用DES 拿到加密工厂
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES);
        //拿到加密key
        SecretKey secretKey = factory.generateSecret(des);

        //cipher实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        cipher.init(Cipher.ENCRYPT_MODE,secretKey,sr);

        return cipher.doFinal(data);
    }

    /**
     * @author zflu
     * @Description   des解密 使用默认key
     * @date 2018/8/17 17:47
     * @param data
     * @return java.lang.String
     */
    public static String decrypt(String data){
        return decrypt(data,DEFAULT_KEY);
    }

    /**
     * @author zflu
     * @Description   des解密 使用key
     * @date 2018/8/17 17:47
     * @param data
     * @return java.lang.String
     */
    public static String decrypt(String data , String key){
        String result = null;
        if(data == null){
            return result;
        }
        try {
            byte[] dataByte = Base64.getDecoder().decode(data);
            byte[] bytes = decryptByte(dataByte,key.getBytes(Contants.CHARSET_NAME));
            result = new String(bytes,Contants.CHARSET_NAME);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   正式的解密方法
     * @date 2018/8/17 17:47
     * @param data
     * @return java.lang.String
     */
    public static byte[] decryptByte(byte[] data , byte[] key) throws Exception{

        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        //从原始密钥数据创建DESKeySpec对象
        DESKeySpec des = new DESKeySpec(key);

        //使用DES 拿到加密工厂
        SecretKeyFactory factory = SecretKeyFactory.getInstance(DES);
        //拿到加密key
        SecretKey secretKey = factory.generateSecret(des);

        //cipher实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        cipher.init(Cipher.DECRYPT_MODE,secretKey,sr);

        return cipher.doFinal(data);
    }

    public static void main(String[] args) {
        String m = "aaaa222";
        String hhahaa = encrypt(m);
        System.out.println(hhahaa);
        String result = decrypt("Sjq+1dduM8s=","luckymao");
        System.out.println(result);
    }


}
